package com.kadron.action;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.creator.KDiscountModelCreator;
import com.kadron.dao.KDiscountDAO;
import com.kadron.dao.KDiscountItemDAO;
import com.kadron.datamodel.KDataModel;
import com.kadron.datamodel.KDiscountModelDataProvider;
import com.kadron.entity.Discount;
import com.kadron.enumeration.DiscountType;
import com.kadron.manager.KMessageManager;
import com.kadron.manager.KSelection;

@AutoCreate
@Scope(ScopeType.PAGE)
@Name("kDiscountListManager")
public class KDiscountListManager extends KMessageManager implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3990213483988851710L;
	
	private static final Log log = LogFactory.getLog(KDiscountListManager.class);
	
	private KDataModel discountList;
	private KDiscountModelDataProvider dataProvider;
	
	@In
	private KDiscountItemDAO discountItemDAO;
	@In
	private KDiscountDAO discountDAO;
	
	@Create
	public void init() {
		log.debug("Init method call: " + this.getClass().getName());
		dataProvider = new KDiscountModelDataProvider(new KDiscountModelCreator(discountDAO), discountDAO);
		discountList = new KDataModel(dataProvider);
	}

	public KDataModel getDiscountList() {
		return discountList;
	}
	
	public String edit(Discount discount) {
		if (discount == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(discount.getId());
		if (DiscountType.PERCENT.equals(discount.getType())) {
			return "e_discount";			
		} else {
			return "e_discount_kadrons";						
		}
	}
	
	public void delete(Discount discount) {
		if (discount == null) {
			bad(KConst.BAD_THING);
			return;
		}
		discountList.remove(discount);
		discountItemDAO.deleteItems(discount.getId());
		discountItemDAO.delete(discount);
	}
}
